package com.hkts.naturalgas.alarminformation.controller;

import com.hkts.naturalgas.utils.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassNameHistoryDataController
 * @Description
 * @Author yck
 * @Date2020-09-16 8:58
 * @Version V1.0
 **/
@RestController
@RequestMapping("/instrumentdata/historyData")
public class HistoryDataController {

    @GetMapping("/getGateData")
    @ResponseBody
    public Result<Object> getGateData(String[] typeParams, String dateType, String[] dateArray) {
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("y_axis", typeParams);
        //今天
        if (dateType.equals("1")) {
            List<String> todayHours = TestDataUtils.getTodayHours();
            map.put("x_axis", todayHours);
            Map<String, Object> resultMap = new HashMap<String, Object>();
            for (int i = 0; i < typeParams.length; i++) {
                List<Float> list = new ArrayList<Float>();
                for (int j = 0; j <= todayHours.size(); j++) {
                    list.add(TestDataUtils.getHoursData());
                }
                resultMap.put(typeParams[i], list);
            }
            map.put("result", resultMap);
        } else if (dateType.equals("2")) {
            map.put("x_axis", TestDataUtils.getListHours(dd.format(TestDataUtils.getYesterday())));
            Map<String, Object> resultMap = new HashMap<String, Object>();
            for (int i = 0; i < typeParams.length; i++) {
                List<Float> list = new ArrayList<Float>();
                for (int j = 0; j < 24; j++) {
                    list.add(TestDataUtils.getHoursData());
                }
                resultMap.put(typeParams[i], list);
            }
            map.put("result", resultMap);
            //近七天
        } else if (dateType.equals("3")) {
            map.put("x_axis", TestDataUtils.getDateArray(7));
            Map<String, Object> resultMap = new HashMap<String, Object>();
            for (int i = 0; i < typeParams.length; i++) {
                List<Float> list = new ArrayList<Float>();
                for (int j = 0; j < 7; j++) {
                    list.add(TestDataUtils.getHoursData());
                }
                resultMap.put(typeParams[i], list);
            }
            map.put("result", resultMap);

        } else {
            List<String> daysByTime = TestDataUtils.getDaysByTime(dateArray);
            map.put("x_axis", daysByTime);
            Map<String, Object> resultMap = new HashMap<String, Object>();
            for (int i = 0; i < typeParams.length; i++) {
                List<Float> list = new ArrayList<Float>();
                for (int j = 0; j < daysByTime.size(); j++) {
                    list.add(TestDataUtils.getHoursData() * 24);
                }
                resultMap.put(typeParams[i], list);
            }
            map.put("result", resultMap);
        }
        return ResultUtil.data(map);
    }

    //获取分页数据
    @GetMapping("/getPageGateData")
    @ResponseBody
    public Result<Object> getPageGateData(String[] typeParams, String dateType, String[] dateArray, CustomPage page,String[] variableArray) {
        //今天
        Map<String,Object> resultData=new HashMap<String,Object>();
        SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        List<String> todayHours = new ArrayList<String>();
        if (dateType.equals("1")) {
            todayHours = TestDataUtils.getTodayHours();
        } else if (dateType.equals("2")) {
            todayHours = TestDataUtils.getListHours(dd.format(TestDataUtils.getYesterday()));
            //近七天
        } else if (dateType.equals("3")) {
            todayHours = TestDataUtils.getDateArray(7);
        } else {
            todayHours = TestDataUtils.getDaysByTime(dateArray);
        }
        int length = typeParams.length*todayHours.size();
        long totaoRows=todayHours.size();
        page.setTotalRows(totaoRows);
        int bag=0;
        Long pageSize = page.getPageSize();
        String filed="filed";
        List<Map<String,Object>> listTable=new ArrayList<Map<String,Object>>();
        Map<String,Object> dateFiled=new HashMap<String,Object>();
        dateFiled.put("index","date");
        dateFiled.put("name","时间");
        listTable.add(dateFiled);
        for(int j=0;j<typeParams.length;j++){
            Map<String,Object> variableFiled=new HashMap<String,Object>();
            variableFiled.put("index",filed+j);
            variableFiled.put("name",typeParams[j]);
            listTable.add(variableFiled);
        }
        resultData.put("tableTitle",listTable);
        for(int i=0;i<todayHours.size();i++){
            if(bag>=(page.getPageSize()*(page.getPageNumber()-1)) && bag <(page.getPageSize()*(
                    page.getPageNumber()))){
                Map<String,Object> row=new HashMap<String,Object>();
                row.put("date",todayHours.get(i));
                for(int j=0;j<typeParams.length;j++){
                    row.put(filed+j,TestDataUtils.getHoursData());
                }
                list.add(row);
            }
            bag++;
        }
        page.setResultData(list);
        resultData.put("page",page);
        return ResultUtil.data(resultData);
    }
   //excle 导出数据
   @GetMapping("/exportHistoryData")
   @ResponseBody
    public void exportHistoryData( HttpServletResponse response,String[] typeParams, String dateType, String[] dateArray) throws Exception{
       SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
       String[] fields=new String[typeParams.length+1];
       for(int i=0;i<fields.length;i++){
           if(i==0){
               fields[i]="时间";
           }else{
               fields[i]=typeParams[i-1];
           }
       }
       List<String> todayHours = new ArrayList<String>();

       if (dateType.equals("1")) {
           todayHours = TestDataUtils.getTodayHours();
       } else if (dateType.equals("2")) {
           todayHours = TestDataUtils.getListHours(dd.format(TestDataUtils.getYesterday()));
           //近七天
       } else if (dateType.equals("3")) {
           todayHours = TestDataUtils.getDateArray(7);
       } else {
           todayHours = TestDataUtils.getDaysByTime(dateArray);
       }
       List<String[]> ll=new ArrayList();
       for(int i=0;i<todayHours.size();i++){
           String[] row=new String[typeParams.length+1];
           row[0]=todayHours.get(i);
           for(int j=0;j<typeParams.length;j++){
               if(dateType.equals("1")){
                   row[j+1]=TestDataUtils.getHoursData()+"";
               }else {
                   row[j+1]=TestDataUtils.getDateData()+"";
               }
           }

           ll.add(row);
       }
       HSSFWorkbook book = ExcelUtil.drawExcel("历史数据", fields, ll);
       ExcelUtil.downExcel(book,response,"站点历史数据");
    }

}
